package com.huang.viewpage;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.viewpagerindicator.PageIndicator;

import java.lang.ref.WeakReference;

public class DotIndicator2 extends View implements ViewPager.OnPageChangeListener {
    private int dotColor = Color.GRAY;
    private int indicatorColor = Color.BLUE;
    private float radius; // 圆角半径
    private int dotCount; // 点的数量
    private int selectedDotIndex = 0; // 当前选中的点索引
    private Paint paint;
    private RectF rect;
    private WeakReference<Context> ctx;
    private float indicatorSpacing;
    private float spacingTmp;
    private float indicatorWith;
    private float ViewWidth;

    private ViewPager2 viewPager2;
    private ViewPager viewPager;

    private void init(Context context, AttributeSet attrs) {
        paint = new Paint();
        paint.setAntiAlias(true);// 默认半径
        this.ctx = new WeakReference<>(context);
//        radius = dp2px(context, 4f);
//        indicatorSpacing = dp2px(ctx.get(), 20f);
//        indicatorWith = dp2px(context, 32f);



        final Resources res = getResources();
        final int mDotColor = res.getColor(R.color.default_dot_indicator_dot_color);
        final int mIndicatorColor = res.getColor(R.color.default_dot_indicator_color);
        final float mIndicatorWith = res.getDimension(R.dimen.default_dot_indicator_with);
        final float mIndicatorSpacing = res.getDimension(R.dimen.default_dot_indicator_spacing);
        final float mRadius = res.getDimension(R.dimen.default_dot_indicator_radius);


        //Retrieve styles attributes
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DotIndicator2);
        indicatorColor = a.getColor(R.styleable.DotIndicator2_indicatorColor,mIndicatorColor);
        dotColor = a.getColor(R.styleable.DotIndicator2_dotColor, mDotColor);
        radius = a.getDimension(R.styleable.DotIndicator2_radius, mRadius);
        indicatorSpacing = a.getDimension(R.styleable.DotIndicator2_indicatorSpacing, mIndicatorSpacing);
        indicatorWith = a.getDimension(R.styleable.DotIndicator2_indicatorWith, mIndicatorWith);

        this.spacingTmp = indicatorSpacing;
        a.recycle();
    }

    public DotIndicator2 setSpacing(float spacing) {
        this.indicatorSpacing =  dp2px(spacing);
        this.spacingTmp = spacing;
        invalidate();
        return this;
    }


    public DotIndicator2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DotIndicator2(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public DotIndicator2(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }
    public void setViewPager2(ViewPager2 pager){
        int count = pager.getAdapter().getItemCount();
        this.dotCount = count;
        this.viewPager2 = pager;
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                setSelectedDotIndex(position); // 更新选中的点
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
        invalidate(); // 触发重绘
    }
    public void setViewPager(ViewPager viewPager){
        this.viewPager = viewPager;
        int count = viewPager.getAdapter().getCount();
        viewPager.addOnPageChangeListener(this);
        this.dotCount = count;
        invalidate(); // 触发重绘
    }

    public void setSelectedDotIndex(int index) {
        this.selectedDotIndex = index;
        invalidate(); // 触发重绘
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        // 计算起始 x 坐标，使其居中
        float startX = ((getWidth() - ViewWidth) / 2) + indicatorWith / 2;
        for (int i = 0; i < dotCount; i++) {
            float cx;
            if (i == 0) {
                cx = startX;
            } else {
                cx = startX + (indicatorWith + spacingTmp) * i;
            } // 计算每个点的 x 坐标
            if (i == selectedDotIndex) {
                // 绘制选中的圆点
                paint.setColor(indicatorColor);
                if (i == 0) {
                    rect = new RectF(startX - indicatorWith / 2, 0, startX + indicatorWith / 2, radius * 2);
                } else {
                    rect = new RectF(cx - indicatorWith / 2, 0, cx + indicatorWith / 2, radius * 2);
                }

                canvas.drawRoundRect(rect, radius, radius, paint);
//                canvas.drawCircle(cx, radius, radius, paint);
            } else {
                // 绘制未选中的圆点
                paint.setColor(dotColor);
                if (i == 0) {
                    canvas.drawCircle(startX, radius, radius, paint);
                } else {
                    canvas.drawCircle(cx , radius, radius, paint);
                }

            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // int width = (int) (dotCount * (spacing) + spacingTmp); // 计算总宽度
        int width = (int) (dotCount * (indicatorWith + spacingTmp) - spacingTmp); // 计算总宽度
        ViewWidth = (dotCount * (indicatorWith + spacingTmp) - spacingTmp); // 计算总宽度
        int height = (int) (radius * 2);
        setMeasuredDimension(width, height);
    }

    protected int dp2px( float dp) {
        final float scale = ctx.get().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setSelectedDotIndex(position); // 更新选中的点
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}